﻿/*
 * DG (DG Gaming Engine)  - API
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.api.common.msg{
	import nanoxml.XMLElement;

	/**
	 * This is a message class which goes to a particular game such as chess,
	 * checkers etc.  The extending class doesn't have to set the game - this is
	 * done behind the scenes.  The gameID should be the game and version
	 * seperated with a colon e.g. chess:0.1
	 *
	 * @author  Bob Marks
	 * @version Beta 0.3
	 */
	public class MsgGameMessage implements ITransmittable {

		private static const NO_STATUS:int=-1;

		/** Integer status of this comm message. */
		protected var status:int;

		/**
		     * Username of the person who sent this message.  This is usually
		     * required to be set if a client needs to know who sent a message
		     * and is set in the server.
		     */
		protected var userid:Number=-1;

		/**
		     * Username of the person who this is being sent to (if empty then no one).
		     */
		protected var userIdTo:Number=-1;

		/** Type of message is used to determine which layer the message is for. */
		public static const XML_ATT_STATUS:String="status";
		public static const XML_ATT_USERID:String="userid";
		public static const XML_ATT_USERID_TO:String="useridTo";

		/**
		     * Constructor with no parameters (no status).
		     */
		public function init():void {
			initFull(NO_STATUS,NO_STATUS);
		}

		/**
		     * Constructor which takes a username and a status.
		     *
		     * @param status    A status
		     */
		public function initStatus(status:int):void {
			initFull(status,NO_STATUS);
		}

		/**
		     * Constructor which only takes a username.
		     *
		     * @param usernameTo
		     */
		public function initUserId(userid:Number):void {
			initFull(NO_STATUS,userid);
		}

		/**
		     * Constructor which takes a status and a username to.
		     *
		     * @param status
		     * @param usernameTo
		     */
		public function initFull(status:int,userid:Number):void {
			this.status=status;
			this.userid=userid;
		}

		/**
		     * Constructor which parses the XMLElement into class fields.
		     *
		     * @param message  Element to parse.
		     */
		public function MsgGameMessage(message:XMLElement=null) {
			if (message==null) {
				return;
			}
			this.status=message.getIntAttribute(XML_ATT_STATUS,NO_STATUS);
			this.userid=message.getLongAttribute(XML_ATT_USERID);
			this.userIdTo=message.getLongAttribute(XML_ATT_USERID_TO);
		}

		/**
		     * Return the status.
		     *
		     * @return    Status of user.
		     */
		public function getStatus():int {
			return status;
		}

		/**
		     * Set the optional uesrname variable.
		     *
		     * @param username
		     */
		public function setUserid(userid:Number):void {
			this.userid=userid;
		}

		/**
		     * Return the optional usernameTo variable.
		     *
		     * @return  username
		     */
		public function getUserid():Number {
			return userid;
		}

		/**
		     * Set the optional usernameTo variable.
		     *
		     * @param usernameTo
		     */
		public function setUseridTo(userIdTo:Number):void {
			this.userIdTo=userIdTo;
		}

		/**
		     * Return the optional usernameTo variable.
		     *
		     * @return  username
		     */
		public function getUseridTo():Number {
			return userid;
		}

		/**
		     * Return true / false on the username being set.
		     *
		     * @return   True if username is set.
		     */
		public function isUseridSet():Boolean {
			return userid!=-1;
		}

		/**
		     * Return true / false on the usernameTo being set.
		     *
		     * @return   True if usernameTo is set.
		     */
		public function isUseridToSet():Boolean {
			return userIdTo!=-1;
		}

		/**
		     * Flatten a DG message and indent it.
		     *
		     * @see java.lang.Object#toString()
		     */
		public function toString():String {
			return flatten().toString(true);
		}

		/**
		     * Create a generic (nameless) XMLElement which sub classes can use.
		     *
		     * @param nameName of the XML element.
		     * @return      XML version of this class.
		     */
		public function flattenWithName(name:String):XMLElement {
			var message:XMLElement=new XMLElement  ;
			message.name=name;

			if (status!=NO_STATUS) {
				message.setIntAttribute(XML_ATT_STATUS,status);
			}
			if (isUseridSet()) {
				message.setLongAttribute(XML_ATT_USERID,userid);
			}
			if (isUseridToSet()) {
				message.setLongAttribute(XML_ATT_USERID_TO,userIdTo);
			}

			return message;// return element back to user
		}

		/* INTERFACE com.dg.gaming.api.common.msg.ITransmittable */

		public function flatten():XMLElement {
			return null;
		}
	}
}